<?php
namespace Home\Controller;
use Think\Controller;
class UserAddressController extends Controller 
{
    public function add()
    {
    	if(IS_POST)
    	{
    		$model = D('Admin/UserAddress');
    		if($model->create(I('post.'), 1))
    		{
    			if($id = $model->add())
    			{
    			    if(IS_AJAX){
                        echo json_encode(array('info'=>'添加成功！','status'=>1,'data'=>$model->find($id)));
                    } else {
                        $this->success('添加成功！', U('lst?p='.I('get.p')));
                    }
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}

		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '添加',
			'_page_btn_name' => '列表',
			'_page_btn_link' => U('lst'),
		));
		$this->display('My/address');
    }
    public function edit()
    {
    	if(IS_POST)
    	{
    		$model = D('Admin/UserAddress');
    		if($model->create(I('post.'), 2))
    		{
    			if($model->save() !== FALSE)
    			{
    				$this->success('修改成功！', U('lst'));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
    	$model = D('Admin/UserAddress');
    	$data = $model->search();
        $info = $model->find(I('get.id'));
        if(IS_AJAX){
            echo json_encode($info);
        } else {
            $this->assign(array(
                'info' => $info,
                'data' => $data['data'],
            ));
            $this->display('My/address');
        }
    }
    public function delete()
    {
    	$model = D('Admin/UserAddress');
    	if($model->delete(I('get.id', 0)) !== FALSE)
    	{
    		$this->success('删除成功！', U('lst', array('p' => I('get.p', 1))));
    		exit;
    	}
    	else 
    	{
    		$this->error($model->getError(),'');
    	}
    }
    public function lst()
    {
        $memberId = session('m_id');
        //echo $memberId;exit;
    	$model = D('Admin/UserAddress');
    	$data = $model
        ->search();//var_dump($data);exit;

        /*if(IS_AJAX){
            $model = D('Admin/UserAddress');
            $info = $model->relation(true)->find(I('get.address_id'));
            exit(json_encode($info));
        }*/
    	$this->assign(array(
    		'data' => $data['data'],
    	));
        if( I('get.type') ){
            exit( json_encode($data['data']) );
        }else{
            // 设置页面中的信息
            $this->assign(array(
                '_page_title' => '列表',
                '_page_btn_name' => '添加',
                '_page_btn_link' => U('add'),
            ));
            $this->display('My/address');
        }
    }

    public function region(){
        $regionId = I('get.id',1);
        $regionModel = D('Region');
        $regionData = $regionModel->where(array(
            'parent_id' => array('eq',$regionId),
        ))->select();
        echo json_encode($regionData);
    }

    //设置默认地址
    public function setDefault(){
        $id = I('get.id',0);
        $m_id = session('m_id');
        $model = D('Admin/UserAddress');
        $model->where(array(
            'member_id' => array('eq',$m_id),
        ))->save(array('is_default' => 0));
        $model->where(array(
            'id' => array('eq',$id),
            'member_id' => array('eq',$m_id),
        ))->save(array('is_default' => 1));
        $this->success('设置成功！',U('lst'));
    }

}